﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ajayumi.develop.win32.Transport.AppUpgrade.Controls;
using System.Diagnostics;
using ajayumi.develop.win32.Transport.Utils.WinForm;

namespace ajayumi.develop.win32.Transport.AppUpgrade
{
    /// <summary>
    /// Author  : 黄乙冬
    /// Date    : 2011-08-31
    /// </summary>
    public partial class WizardForm : Form
    {
        #region variable

        private UcBase m_Base = null;

        #endregion

        public WizardForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 设置标题
        /// </summary>
        /// <param name="text"></param>
        protected virtual void SetTitle(string text)
        {
            this.lbTitle.Text = text;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.InitFirstUserControl();
        }

        #region method

        /// <summary>
        /// 初始化第一个用户控件
        /// </summary>
        private void InitFirstUserControl()
        {
            this.UpdateContainer(new UcCheckUpgrade());
        }

        /// <summary>
        /// 更新容器
        /// </summary>
        /// <param name="ucBase"></param>
        private void UpdateContainer(UcBase ucBase)
        {
            if (ucBase == null)
            { return; }

            if (this.m_Base != null)
            {
                this.tableLayoutPanel1.Invoke(new Action<Control>(this.tableLayoutPanel1.Controls.Remove), new object[] { this.m_Base });
            }

            this.m_Base = ucBase;
            this.m_Base.SetButtonVisible = this.SetBtnVisible;

            ControlHelper.SetText(this.lbTitle, ucBase.Title);

            ucBase.Dock = DockStyle.Fill;
            this.tableLayoutPanel1.Invoke(new Action<Control, int, int>(this.tableLayoutPanel1.Controls.Add), new object[] { ucBase, 0, 1 });
        }

        internal void PerformBack()
        {
            this.UpdateContainer(this.m_Base.Back());
        }

        /// <summary>
        /// 模拟实现下一步
        /// </summary>
        internal void PerformNext()
        {
            this.UpdateContainer(this.m_Base.Next());
        }

        internal void SetBtnVisible(WizardFormButtons buttons)
        {
            ControlHelper.SetVisible(this.btnRestore, (buttons & WizardFormButtons.Restore).Equals(WizardFormButtons.Restore));
            ControlHelper.SetVisible(this.btnUpgrade, (buttons & WizardFormButtons.Upgrade).Equals(WizardFormButtons.Upgrade));
            ControlHelper.SetVisible(this.btnCancel, (buttons & WizardFormButtons.Cancel).Equals(WizardFormButtons.Cancel));
            ControlHelper.SetVisible(this.btnExit, (buttons & WizardFormButtons.Exit).Equals(WizardFormButtons.Exit));
        }

        #endregion


        private void btnUpgrade_Click(object sender, EventArgs e)
        {
            this.m_Base.Upgrade();
            this.PerformNext();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.m_Base.Cancel();
        }

        private void btnRestore_Click(object sender, EventArgs e)
        {
            this.m_Base.Restore();
            this.PerformNext();
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
            this.m_Base.AppExit();
        }
    }
}
